var city = "";
let timer = null;
function realoadShowAll() {
    clearTimeout(timer);
    timer = setTimeout(function () {
        city = $(".pas_active_BaFwc").html();
        showAll(city);
    }, 10);
}

$(".hs_search-btn-container_R0HuJ").click(function () {
    location.href="../searchHotel/searchHotel.html";
});

$(function () {
    var user = JSON.parse(sessionStorage.getItem("loginUser"));
    if (user!="" && user!= null){
        $("#loginName").html(user.userName);
        city = $(".pas_active_BaFwc").html();
        realoadShowAll();
    }else{
        location.href="../../login.html";
    }
});

$(".pas_btn_XmTzE").click(function () {
    realoadShowAll();
});

function showAll(city) {
    $.ajax({
        url: urlPlatfrom + "hotel/hotelRecommend",
        headers: {"token": sessionStorage.getItem("token")},
        type:"get",
        data:{"city":city},
        success:function (data) {
            if (data.success){
                table_hotel(data.data);
            }else{
                location.href="../../login.html";
            }
        }
    });
}

var length = 0;
function table_hotel(data) {
    $(".pas_hotel_8EyaF").empty();
    length = data.length;
    for (var i = 0; i < data.length; i++) {
        var sum = data.length - i;
        if(sum <= 1){
            createDocument(data,i);
        }else{
            if (parseInt(data[i].order) < parseInt(data[i+1].order)){
                createDocument(data,i);
            }else{
                createDocument(data,i+1);
            }
        }
    }
}

function createDocument(data,i) {
    var star = "";
    for (var j = 1; j <= data[i].hotelLevel; j++) {
        star += "<p style='display:inline-block;width: 8px;height: 8px;border-radius: 50%;background-color: #eea236;border: 1px solid #eea236;'></p>";
    }
    var document = "<li>" +
        "<div>" +
        "<input type='hidden' value="+data[i].id+">" +
        "<p id='recommend_p'><img id='recommend_img' src="+data[i].hotelImg+" /></p>" +
        "<h4>"+data[i].hotelName+"</h4>" +
        "<div style='padding-left:15px;width: 205px;height:15px;'>" +star+ "</div>" +
        "<div id='scoreParent'><span id='score'>"+data[i].grade.toFixed(1)+"<span style='color: #c4e3f3;font-weight: normal;font-size: 13px;'>/5</span></span><span style='color: #3BA2F8;margin-left: 10px;font-size: 15px;'>超棒</span></div>" +
        "<div><span id='comments'><span class='commentCount'>"+data[i].commentCount+"</span>条点评</span><span style='color: #0086F6;font-weight: bold;font-size: 15px;margin-left: 90px;'>￥"+data[i].price+"</span></div>" +
        "</div>" +
        "</li>";
    $(".pas_hotel_8EyaF").append(document);
}

var pas_right_clickSum = 0;
function pas_right_Z1g0E_clickByHidden() {
    alert(pas_right_clickSum);
    alert(length % 3);
    if (length % 3 >= pas_right_clickSum){
        pas_right_clickSum++;
    }else{
        $(".pas_right_Z1g0E").addClass("pas_hidden_EArZH");
        pas_right_clickSum = 0;
    }
}
$("#pas_right").click(function () {
    pas_right_Z1g0E_clickByHidden();
});


$(".searchBtn").click(function () {
    sessionStorage.setItem("cityName",$("#hotels-destination").val());
    sessionStorage.setItem("hotelName",$("#keyword").val());
    location.href="../searchHotel/searchHotel.html";
})